﻿package kit4j.ui.cs.full;

import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;

/**
 * FullComponent接口的委托实现类。实现FullComponent接口的类型可将FullComponent接口的大部分操作委托给该类型的实例执行。
 * 
 * @author MiniKnife
 * 
 */
public class FullComponentHelper implements FullComponent {

	/**
	 * 需要委托操作的组件。
	 */
	private Component component = null;

	/**
	 * 背景图像。
	 */
	private Image backgroundImage = null;

	/**
	 * 背景图像的定位属性。
	 */
	private BackgroundConstraints backgroundConstraints = null;

	/**
	 * 是否自动为组件的子组件自动生成恰当的背景图像。当且仅当组件为Container类型的对象时有效。
	 */
	private boolean autoGenerateComponentsBackgroundImage = false;

	public FullComponentHelper(Component component) {
		if (component == null) {
			throw new NullPointerException("component is null.");
		}
		this.component = component;
	}

	/** 
	 * @see kit4j.ui.cs.full.FullComponent#setBackgroundImage(java.awt.Image)
	 */
	@Override
	public void setBackgroundImage(Image img) {
		this.backgroundImage = img;

		updateComponentsBackgroundImage();
		repaintComponent();
	}

	/** 
	 * @see kit4j.ui.cs.full.FullComponent#getBackgroundImage()
	 */
	@Override
	public Image getBackgroundImage() {
		return this.backgroundImage;
	}

	/** 
	 * @see kit4j.ui.cs.full.FullComponent#setBackgroundConstraints(kit4j.ui.cs.full.BackgroundConstraints)
	 */
	@Override
	public void setBackgroundConstraints(BackgroundConstraints bgc) {
		this.backgroundConstraints = bgc;

		updateComponentsBackgroundImage();
		repaintComponent();
	}

	/** 
	 * @see kit4j.ui.cs.full.FullComponent#getBackgroundConstraints()
	 */
	@Override
	public BackgroundConstraints getBackgroundConstraints() {
		return this.backgroundConstraints;
	}

	/** 
	 * @see kit4j.ui.cs.full.FullComponent#setAutoGenerateComponentsBackgroundImage(boolean)
	 */
	@Override
	public void setAutoGenerateComponentsBackgroundImage(boolean autoGenerateComponentsBackgroundImage) {
		this.autoGenerateComponentsBackgroundImage = autoGenerateComponentsBackgroundImage;

		updateComponentsBackgroundImage();
	}

	/**
	 * 更新子组件的背景图像（如果需要）。
	 */
	public void updateComponentsBackgroundImage() {
		if (this.backgroundImage != null && isAutoGenerateComponentsBackgroundImage() && component instanceof Container) {
			FullComponentBackgroundPaintHelper.setComponentsBackground((Container) this.component,
					this.getBackgroundImage(), this.getBackgroundConstraints());
		}
	}

	/** 
	 * @see kit4j.ui.cs.full.FullComponent#isAutoGenerateComponentsBackgroundImage()
	 */
	@Override
	public boolean isAutoGenerateComponentsBackgroundImage() {
		return this.autoGenerateComponentsBackgroundImage;
	}

	/**
	 * 为组件绘制背景图像。持有该对象的组件可通过调用此方法委托绘制背景图像。
	 * 
	 * @param g
	 *            组件的图形上下文对象
	 */
	public void paintBackgroundImage(Graphics g) {
		if (this.backgroundImage != null) {
			FullComponentBackgroundPaintHelper.paintBackground(component, backgroundImage, backgroundConstraints, g);

			updateComponentsBackgroundImage();
		}
	}

	protected void repaintComponent() {
		if (this.component.isVisible()) {
			this.component.repaint();
		}
	}

}
